#include "CKentauros_Attack_Effect.h"
#include "CUnit.h"

#include "Enum.h"

#include "Utility.h"
#include "Animation.h"
#include "SpriteMaker.h"

int const KENTAUROS_ATTACK_EFFECT_HLAF_WIDTH = 30;
int const KENTAUROS_ATTACK_EFFECT_HLAF_HEIGHT = -10;
int const KENTAUROS_ATTACK_EFFECT_MOVE_DELAY = 1000;
int const KENTAUROS_ATTACK_EFFECT_MOVE_X = -20;

int const DOWN_Y = 20;

CKentauros_Attack_Effect::CKentauros_Attack_Effect(int _nX, int _nY, int _nTargetX, int _nDamage, ls_Unit* _pEnumy, CBuilding* _pBuilding, int _nNumber)
{
	m_nX = _nX - KENTAUROS_ATTACK_EFFECT_HLAF_WIDTH;
	m_nY = _nY - DOWN_Y;
	m_nTargetX = _nTargetX;
	m_nDamage = _nDamage;
	m_pEnumy = _pEnumy;
	m_nNumber = _nNumber;
	m_pEnumyBuilding = _pBuilding;

	SharedSpriteMaker.createSprite("Kentauros_Attack_Effect", m_nNumber);
	SharedSpriteMaker.getSprite(m_nNumber)->setSize(KENTAUROS_ATTACK_EFFECT_HLAF_WIDTH, (-KENTAUROS_ATTACK_EFFECT_HLAF_HEIGHT));
	SharedSpriteMaker.getSprite(m_nNumber)->setPosition(-100000, 100000, m_nNumber);

	m_nFinalTime = OgrePlugin_2DRenderer::getPlatformTime() + KENTAUROS_ATTACK_EFFECT_MOVE_DELAY;
}

CKentauros_Attack_Effect::~CKentauros_Attack_Effect(void)
{
	SharedSpriteMaker.removeSprite(m_nNumber);
}

void CKentauros_Attack_Effect::put(void)
{
	m_nNowTime = OgrePlugin_2DRenderer::getPlatformTime();

	if(0 >= m_nFinalTime - m_nNowTime)
	{
		m_nX += KENTAUROS_ATTACK_EFFECT_MOVE_X;

		m_nFinalTime = OgrePlugin_2DRenderer::getPlatformTime() + KENTAUROS_ATTACK_EFFECT_MOVE_DELAY;
	}

	SharedSpriteMaker.getSprite(m_nNumber)->setPosition(m_nX, m_nY, 100);
}

int CKentauros_Attack_Effect::run(void)
{
	m_itr = m_pEnumy->begin();
	while(m_itr != m_pEnumy->end())
	{
		if(m_nX <= (*m_itr)->GetX())
		{
			if(m_nY + DOWN_Y == (*m_itr)->GetY())
			{
				(*m_itr)->Hit(m_nDamage, eMagician);
				return -1;
			}
		}
		++m_itr;
	}

	if(m_nX <= m_pEnumyBuilding->GetX())
	{
		m_pEnumyBuilding->Hit(m_nDamage);
		return -1;
	}
	return 1;
}